package com.llsydn.ui.component.menu;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.*;
import android.widget.Button;
import android.widget.Toast;
import com.llsydn.hello.R;

/**
 * @author lilinshen
 * @title menu菜单component组件
 * @description 请填写相关描述
 * @date 2018/9/20 13:47
 */
public class MenuComponentActivity extends AppCompatActivity {
    /**
     * 一、OptionMenu
     * 1.如何触发Menu的显示?  点击menu键
     * 2.如何向Menu中添加MenuItem?  重写onCreateOptionMenu
     * 3.选择某个MenuItem时如何响应?
     * <p>
     * 二、ContextMenu : 上下文菜单
     * 1.为某个视图添加创建ContextMenu的监听(需要长按触发)  设置监听setOnCreateContextMenuListener
     * 2.显示菜单的回调方法  重写onCreateContextMenu
     * 3.当选择某个菜单项的回调方法 onContextItemSelected(MenuItem item)
     */

    private Button btn_menu_1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);

        btn_menu_1 = findViewById(R.id.btn_menu_1);
        btn_menu_1.setOnCreateContextMenuListener(this);
    }

    /*****************menu键(OptionMenu)****************************/
    /**
     * 显示OptionMenu的回调方法, 在此方法中向Menu中添加MenuItem
     *
     * @param menu
     * @return
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //1.编码方式添加菜单
        //分组，菜单id，显示顺序，菜单名称
        //menu.add(0, 2, 0, "添加");
        //menu.add(0, 3, 0, "删除");

        //2.加载menu文件的方式:
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.option_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    /**
     * 当选择某个菜单项的回调方法
     *
     * @param item
     * @return
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.item_add:
                Toast.makeText(MenuComponentActivity.this, "添加2", Toast.LENGTH_SHORT).show();
                break;
            case R.id.item_del:
                Toast.makeText(MenuComponentActivity.this, "删除2", Toast.LENGTH_SHORT).show();
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    /******************长按键(ContextMenu)***************************/

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        menu.add(0, 4, 0, "添加");
        menu.add(0, 5, 0, "删除");
        super.onCreateContextMenu(menu, v, menuInfo);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case 4:
                Toast.makeText(MenuComponentActivity.this, "添加", Toast.LENGTH_SHORT).show();
                break;
            case 5:
                Toast.makeText(MenuComponentActivity.this, "删除", Toast.LENGTH_SHORT).show();
                break;
            default:
                break;
        }
        return super.onContextItemSelected(item);
    }
}
